package com.liangfeng.searchdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText et_search;
    private ListView lv_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_search = (EditText) findViewById(R.id.et_search);
        lv_search = (ListView) findViewById(R.id.lv_search);

        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            strings.add("item" + i);
        }

        //开启过滤
        lv_search.setTextFilterEnabled(true);
        lv_search.setAdapter(new MyAdapter(this, strings));
        //监听输入框的动作
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString().trim()))
                    lv_search.clearTextFilter();//搜索文本为空时，清除ListView的过滤
                else
                    lv_search.setFilterText(s.toString().trim());//设置过滤关键字
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }



    class MyAdapter extends BaseAdapter implements Filterable {
        List<String> datas;
        List<String> backDatas;//备份原始数据
        Context context;
        MyFilter mFilter;

        public MyAdapter(Context context, List list) {
            datas = list;
            backDatas = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item, null);
            TextView tv_seach = (TextView) inflate.findViewById(R.id.tv_seach);
            tv_seach.setText(datas.get(position));
            return tv_seach;
        }

        //当ListView调用setTextFilter()方法的时候，便会调用该方法
        @Override
        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new MyFilter();
            }
            return mFilter;
        }

        class MyFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {//设置过滤规则
                FilterResults filterResults = new FilterResults();
                List<String> list;
                if (TextUtils.isEmpty(constraint)) {
                    list = backDatas;//输入框内无内容,显示所有数据
                } else {
                    //将符合条件的数据放到集合中
                    list = new ArrayList<>();
                    for (String data : backDatas) {
                        if (data.contains(constraint)) {
                            list.add(data);
                        }
                    }
                }

                filterResults.values = list; //将得到的集合保存到FilterResults的value变量中
                filterResults.count = list.size();//将集合的大小保存到FilterResults的count变量中
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {//通知适配器更新数据
                datas = (List<String>) results.values;
                if (results.count >0){
                    //刷新数据
                    notifyDataSetChanged();
                    LogUtils.e("publishResults------>notifyDataSetChanged");
                }else {
                    notifyDataSetInvalidated();//通知数据失效
                    LogUtils.e("publishResults------>notifyDataSetInvalidated");
                }
            }
        }
    }
}
